package com.zeus.base.smscenter.restful.api.jms;

import com.google.common.base.Strings;
import com.aliyun.openservices.ons.api.*;

import java.util.Properties;

/**
 * Created by Administrator on 2016-03-24.
 */
public class JmsProducer {
    private String producerId;

    private String accessKey;

    private String secretKey;

    private String topicName;

    private String tagName;

    private Producer producer;

    public void onStart(){
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ProducerId, producerId);
        properties.put(PropertyKeyConst.AccessKey, accessKey);
        properties.put(PropertyKeyConst.SecretKey, secretKey);
        producer = ONSFactory.createProducer(properties);
        producer.start();
    }

    public void onStop(){
        producer.shutdown();
        System.out.println("关闭ONS");
    }

    public String sendMessage(String topicName,String tagName,String message,String businessId){
        if(Strings.isNullOrEmpty(topicName)){
            topicName=this.topicName;
        }
        if(Strings.isNullOrEmpty(tagName)){
            tagName=this.tagName;
        }
        Message msg = new Message(topicName,tagName,message.getBytes());
        msg.setKey(businessId);
        SendResult sendResult = producer.send(msg);
        return  sendResult.getMessageId();
    }


    public String getProducerId() {
        return producerId;
    }

    public void setProducerId(String producerId) {
        this.producerId = producerId;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getTopicName() {
        return topicName;
    }

    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }
}
